<template>
  <van-skeleton :row="20" :loading="!roomDetailInfo?.id">
    <div class="room-detail-page">
      <!-- 轮播图区域 -->
      <div class="swipe-container">
        <van-swipe 
          class="room-swipe" 
          :autoplay="4000" 
          indicator-color="rgba(255, 255, 255, 0.8)"
          :show-indicators="roomDetailInfo.graphVoList?.length > 1"
        >
          <van-swipe-item
            v-for="(item, index) in roomDetailInfo.graphVoList"
            :key="item.url"
          >
            <div class="swipe-item-wrapper">
              <van-image 
                fit="cover" 
                :src="item.url" 
                class="room-image"
              >
                <template v-slot:error>
                  <div class="image-error">
                    <van-icon name="photo-fail" size="32" color="#ccc" />
                    <span>图片加载失败</span>
                  </div>
                </template>
                <template v-slot:loading>
                  <div class="image-loading">
                    <van-loading type="spinner" size="24" color="#fff" />
                  </div>
                </template>
              </van-image>
              <!-- 渐变遮罩 -->
              <div class="image-overlay"></div>
            </div>
          </van-swipe-item>
        </van-swipe>
        
        <!-- 图片数量指示器 -->
        <div class="image-counter" v-if="roomDetailInfo.graphVoList?.length > 1">
          <van-icon name="photo" />
          <span>{{ roomDetailInfo.graphVoList?.length || 0 }}</span>
        </div>
      </div>

      <!-- 房间信息卡片 -->
      <div class="room-info-card">
        <div class="room-header">
          <h1 class="room-title">
            {{ `${roomDetailInfo.apartmentItemVo?.name || ""} ${roomDetailInfo.roomNumber}房间` }}
          </h1>
          <div class="room-status">
            <van-tag type="success" size="medium" round>可租</van-tag>
          </div>
        </div>
        
        <!-- 标签区域 -->
        <div class="room-tags" v-if="roomDetailInfo.labelInfoList?.length">
          <van-tag
            v-for="item in roomDetailInfo.labelInfoList"
            :key="item.id"
            type="primary"
            plain
            round
            size="medium"
            class="tag-item"
          >
            {{ item.name }}
          </van-tag>
        </div>
        
        <!-- 价格区域 -->
        <div class="price-section">
          <div class="price-main">
            <span class="price-symbol">￥</span>
            <span class="price-amount">{{ roomDetailInfo.rent }}</span>
            <span class="price-unit">/月</span>
          </div>
          <div class="price-note">租金不含水电费</div>
        </div>
      </div>

      <!-- 基本信息 -->
      <div class="info-card">
        <div class="card-header">
          <van-icon name="home-o" size="20" color="#6366f1" />
          <span class="card-title">基本信息</span>
        </div>
        <div class="basic-info-grid">
          <div 
            v-for="item in roomDetailInfo.attrValueVoList"
            :key="item.id"
            class="info-item"
          >
            <div class="info-label">{{ item.attrKeyName }}</div>
            <div class="info-value">{{ item.name }}</div>
          </div>
        </div>
      </div>

      <!-- 配套信息 -->
      <div class="info-card">
        <div class="card-header">
          <van-icon name="setting-o" size="20" color="#6366f1" />
          <span class="card-title">房间设施</span>
        </div>
        <div class="facility-grid">
          <div 
            v-for="item in roomDetailInfo.facilityInfoList"
            :key="item.id"
            class="facility-item"
          >
            <div class="facility-icon">
              <SvgIcon :name="item.icon" size="28" />
            </div>
            <span class="facility-name">{{ item.name }}</span>
          </div>
        </div>
      </div>

      <!-- 位置详情 -->
      <div class="info-card">
        <div class="card-header">
          <van-icon name="location-o" size="20" color="#6366f1" />
          <span class="card-title">位置信息</span>
        </div>
        <div class="location-info">
          <p class="address-text">{{ roomDetailInfo.apartmentItemVo?.addressDetail }}</p>
          <div class="map-container">
            <div id="container" class="map-view"></div>
          </div>
        </div>
      </div>

      <!-- 费用明细 -->
      <div class="info-card">
        <div class="card-header">
          <van-icon name="gold-coin-o" size="20" color="#6366f1" />
          <span class="card-title">费用详情</span>
        </div>
        <div class="fee-list">
          <div class="fee-header">
            <div class="fee-col">费用科目</div>
            <div class="fee-col">收费标准</div>
          </div>
          <div 
            v-for="item in roomDetailInfo.feeValueVoList"
            :key="item.id"
            class="fee-item"
          >
            <div class="fee-name">{{ item.feeKeyName }}</div>
            <div class="fee-value">￥{{ item.name }}{{ item.unit }}</div>
          </div>
        </div>
      </div>

      <!-- 付款方式 -->
      <div class="info-card">
        <div class="card-header">
          <van-icon name="credit-pay" size="20" color="#6366f1" />
          <span class="card-title">支付方式</span>
        </div>
        <div class="payment-list">
          <div 
            v-for="item in roomDetailInfo.paymentTypeList"
            :key="item.id"
            class="payment-item"
          >
            <div class="payment-name">{{ item.name }}</div>
            <div class="payment-desc">{{ item.additionalInfo }}</div>
          </div>
        </div>
      </div>

      <!-- 可选租期 -->
      <div class="info-card">
        <div class="card-header">
          <van-icon name="calendar-o" size="20" color="#6366f1" />
          <span class="card-title">可选租期</span>
        </div>
        <div class="lease-list">
          <div 
            v-for="item in roomDetailInfo.leaseTermList"
            :key="item.id"
            class="lease-item"
          >
            <div class="lease-term">{{ item.monthCount }}{{ item.unit }}</div>
            <div class="lease-desc">到期可续</div>
          </div>
        </div>
      </div>

      <!-- 所属公寓 -->
      <div class="info-card apartment-card">
        <div class="card-header">
          <van-icon name="building-o" size="20" color="#277941" />
          <span class="card-title">所属公寓</span>
        </div>
        <div class="modern-apartment-card" @click="goApartmentDetail">
          <!-- 公寓图片 -->
          <div class="apartment-image-container">
            <van-image
              class="apartment-image"
              :src="roomDetailInfo.apartmentItemVo?.graphVoList?.[0]?.url"
              fit="cover"
            >
              <template v-slot:error>
                <div class="image-error">
                  <van-icon name="photo-fail" size="24" color="#ccc" />
                </div>
              </template>
              <template v-slot:loading>
                <div class="image-loading">
                  <van-loading type="spinner" size="20" color="#fff" />
                </div>
              </template>
            </van-image>
            <!-- 图片遮罩 -->
            <div class="image-overlay"></div>
          </div>
          
          <!-- 公寓信息 -->
          <div class="apartment-info">
            <div class="apartment-header">
              <h3 class="apartment-name">{{ roomDetailInfo.apartmentItemVo?.name }}</h3>
              <div class="apartment-price">
                <span class="price-symbol">￥</span>
                <span class="price-amount">{{ roomDetailInfo.apartmentItemVo?.minRent }}</span>
                <span class="price-unit">/月起</span>
              </div>
            </div>
            
            <div class="apartment-address">
              <van-icon name="location-o" size="14" color="#666" />
              <span>{{ roomDetailInfo.apartmentItemVo?.provinceName }} {{ roomDetailInfo.apartmentItemVo?.cityName }} {{ roomDetailInfo.apartmentItemVo?.districtName }}</span>
            </div>
            
            <!-- 公寓标签 -->
            <div class="apartment-tags" v-if="roomDetailInfo.apartmentItemVo?.labelInfoList?.length">
              <van-tag
                v-for="item in roomDetailInfo.apartmentItemVo.labelInfoList.slice(0, 3)"
                :key="item.id"
                type="primary"
                plain
                round
                size="small"
                class="apartment-tag"
              >
                {{ item.name }}
              </van-tag>
              <span v-if="roomDetailInfo.apartmentItemVo.labelInfoList.length > 3" class="more-tags">
                +{{ roomDetailInfo.apartmentItemVo.labelInfoList.length - 3 }}
              </span>
            </div>
            
            <!-- 查看详情按钮 -->
            <div class="view-detail-btn">
              <van-icon name="arrow" size="16" color="#277941" />
              <span>查看详情</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 底部间距 -->
      <div class="bottom-spacer"></div>

      <!-- 预约看房按钮 -->
      <van-sticky :offset-bottom="0" position="bottom">
        <div class="action-bar">
          <van-button 
            type="primary" 
            size="large"
            round
            block 
            @click="appointmentToViewHandle"
            class="appointment-btn"
          >
            <van-icon name="calendar-o" class="btn-icon" />
            预约看房
          </van-button>
        </div>
      </van-sticky>
    </div>
  </van-skeleton>
</template>
<script setup lang="ts">
import { getRoomDetailById } from "@/api/search";
import { onMounted, ref } from "vue";
import type { RoomDetailInterface } from "@/api/search/types";
import { useMap } from "@/hooks/useMap";
import poiMarkerRed from "@/assets/poi-marker-red.png";

import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
// 房间的详情信息
const roomDetailInfo = ref<RoomDetailInterface>({} as RoomDetailInterface);
// 获取房间的详情信息
const getRoomDetailHandle = async () => {
  const { data } = await getRoomDetailById(route.query.id as string);
  roomDetailInfo.value = data;
};
//#region <高德地图相关>
// 地图实例
const { AMap, initMap } = useMap();
function initMapPage(opts?: { lng: number; lat: number }) {
  console.log("opts", opts);
  console.log("AMap", AMap.value);
  const map = new AMap.value.Map("container", {
    zoom: 19, //初始地图级别
    center: [opts?.lng, opts?.lat], //初始地图中心点
    showIndoorMap: false //关闭室内地图
  });
  const icon = new AMap.value.Icon({
    size: new AMap.value.Size(25, 34), // 图标尺寸
    image: poiMarkerRed, // Icon的图像
    imageSize: new AMap.value.Size(25, 34)
  });
  const marker = new AMap.value.Marker({
    icon: icon,
    position: [opts?.lng, opts?.lat],
    anchor: "bottom-center"
  });
  map.add(marker);
  map.setFitView();
}
// 预约看房
const appointmentToViewHandle = () => {
  console.log("appointmentToViewHandle");
  router.push({
    path: "/appointment",
    query: { apartmentId: roomDetailInfo.value.apartmentId }
  });
};

// 跳转到公寓详情页面
const goApartmentDetail = () => {
  router.push({ 
    path: "/apartmentDetail", 
    query: { id: roomDetailInfo.value.apartmentItemVo?.id } 
  });
};
//#endregion
onMounted(async () => {
  await getRoomDetailHandle();
  console.log(roomDetailInfo.value.apartmentItemVo.longitude);
  await initMap();
  initMapPage({
    lng: +roomDetailInfo.value.apartmentItemVo.longitude,
    lat: +roomDetailInfo.value.apartmentItemVo.latitude
  });
});
</script>

<style scoped lang="less">
.room-detail-page {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  min-height: 100vh;
}

// 轮播图区域
.swipe-container {
  position: relative;
  height: 40vh;
  overflow: hidden;
}

.room-swipe {
  height: 100%;
  border-radius: 0 0 24px 24px;
  overflow: hidden;
}

.swipe-item-wrapper {
  position: relative;
  height: 100%;
}

.room-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.3));
  pointer-events: none;
}

.image-error,
.image-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: #f5f5f5;
  color: #999;
  gap: 8px;
}

.image-counter {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 6px 12px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  backdrop-filter: blur(4px);
}

// 房间信息卡片
.room-info-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(15px);
  margin: -20px 16px 16px;
  border-radius: 24px;
  padding: 28px;
  box-shadow: 0 12px 40px rgba(102, 126, 234, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  position: relative;
  z-index: 2;
}

.room-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.room-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
  flex: 1;
  line-height: 1.4;
}

.room-status {
  margin-left: 12px;
}

.room-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.tag-item {
  background: linear-gradient(135deg, #8b5cf6, #a855f7);
  color: white;
  padding: 8px 16px;
  border-radius: 24px;
  font-size: 13px;
  margin-right: 8px;
  margin-bottom: 8px;
  box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
  font-weight: 500;
  border: none;
}

.price-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.price-main {
  display: flex;
  align-items: baseline;
  gap: 2px;
}

.price-symbol {
  font-size: 18px;
  color: #8b5cf6;
  font-weight: 500;
}

.price-amount {
  font-size: 28px;
  color: #8b5cf6;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(139, 92, 246, 0.2);
}

.price-unit {
  font-size: 16px;
  color: #8b5cf6;
  font-weight: 500;
}

.price-note {
  font-size: 12px;
  color: #999;
  align-self: flex-end;
}

// 信息卡片
.info-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(15px);
  margin: 0 16px 16px;
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
}

// 基本信息网格
.basic-info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.info-item {
  background: #f8f9fa;
  padding: 12px;
  border-radius: 12px;
  border-left: 3px solid #277941;
}

.info-label {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.info-value {
  font-size: 14px;
  color: #1a1a1a;
  font-weight: 500;
}

// 配套设施网格
.facility-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.facility-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 16px 8px;
  background: #f8f9fa;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.facility-item:hover {
  background: #e8f5e8;
  transform: translateY(-2px);
}

.facility-icon {
  margin-bottom: 8px;
  color: #277941;
}

.facility-name {
  font-size: 12px;
  color: #666;
  line-height: 1.2;
}

// 位置信息
.location-info {
  .address-text {
    font-size: 14px;
    color: #666;
    margin: 0 0 16px 0;
    line-height: 1.5;
  }
}

.map-container {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.map-view {
  width: 100%;
  height: 200px;
}

// 费用列表
.fee-list {
  .fee-header {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    padding: 12px 0;
    border-bottom: 2px solid #277941;
    margin-bottom: 12px;
  }

  .fee-col {
    font-size: 14px;
    font-weight: 600;
    color: #277941;
    text-align: center;
  }

  .fee-item {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;
    
    &:last-child {
      border-bottom: none;
    }
  }

  .fee-name {
    font-size: 14px;
    color: #1a1a1a;
  }

  .fee-value {
    font-size: 14px;
    color: #ff4757;
    font-weight: 500;
    text-align: right;
  }
}

// 付款方式列表
.payment-list {
  .payment-item {
    padding: 16px;
    background: #f8f9fa;
    border-radius: 12px;
    margin-bottom: 12px;
    border-left: 4px solid #277941;
    
    &:last-child {
      margin-bottom: 0;
    }
  }

  .payment-name {
    font-size: 14px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 4px;
  }

  .payment-desc {
    font-size: 12px;
    color: #666;
  }
}

// 租期列表
.lease-list {
  .lease-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    background: #f8f9fa;
    border-radius: 12px;
    margin-bottom: 12px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }

  .lease-term {
    font-size: 14px;
    font-weight: 600;
    color: #277941;
  }

  .lease-desc {
    font-size: 12px;
    color: #666;
  }
}

// 公寓卡片
.apartment-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(15px);
  margin: 0 16px 16px;
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  
  .apartment-content {
    margin: -4px;
  }
}

/* 现代化公寓卡片样式 */
.modern-apartment-card {
  position: relative;
  background: linear-gradient(135deg, #f8fffe 0%, #f0f9ff 100%);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(39, 121, 65, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid rgba(39, 121, 65, 0.1);
}

.modern-apartment-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(39, 121, 65, 0.15);
}

.modern-apartment-card:active {
  transform: translateY(0);
}

/* 公寓图片容器 */
.apartment-image-container {
  position: relative;
  width: 100%;
  height: 160px;
  overflow: hidden;
}

.apartment-image {
  width: 100%;
  height: 100%;
  border-radius: 0;
}

.apartment-image :deep(.van-image__img) {
  transition: transform 0.3s ease;
}

.modern-apartment-card:hover .apartment-image :deep(.van-image__img) {
  transform: scale(1.05);
}

.apartment-image-container .image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.1) 100%);
  pointer-events: none;
}

.apartment-image-container .image-error,
.apartment-image-container .image-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: #f5f5f5;
}

/* 公寓信息区域 */
.apartment-info {
  padding: 16px;
}

.apartment-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.apartment-name {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
  line-height: 1.3;
  flex: 1;
  margin-right: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.apartment-price {
  display: flex;
  align-items: baseline;
  color: #277941;
  font-weight: 600;
  flex-shrink: 0;
}

.apartment-price .price-symbol {
  font-size: 14px;
  margin-right: 2px;
}

.apartment-price .price-amount {
  font-size: 20px;
  font-weight: 700;
}

.apartment-price .price-unit {
  font-size: 12px;
  color: #666;
  margin-left: 2px;
}

.apartment-address {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  color: #666;
  font-size: 14px;
}

.apartment-address span {
  margin-left: 4px;
  line-height: 1.4;
}

/* 公寓标签 */
.apartment-tags {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}

.apartment-tag {
  background: rgba(39, 121, 65, 0.08) !important;
  border-color: rgba(39, 121, 65, 0.2) !important;
  color: #277941 !important;
  font-size: 11px;
  padding: 2px 8px;
  height: 22px;
  line-height: 18px;
}

.more-tags {
  font-size: 12px;
  color: #999;
  background: #f5f5f5;
  padding: 2px 6px;
  border-radius: 10px;
  height: 22px;
  line-height: 18px;
  display: flex;
  align-items: center;
}

/* 查看详情按钮 */
.view-detail-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: #277941;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 16px;
  background: rgba(39, 121, 65, 0.08);
  border-radius: 20px;
  transition: all 0.2s ease;
  width: fit-content;
  margin: 0 auto;
}

.modern-apartment-card:hover .view-detail-btn {
  background: rgba(39, 121, 65, 0.12);
  transform: translateX(2px);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .apartment-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .apartment-name {
    margin-right: 0;
    white-space: normal;
    line-height: 1.4;
  }
  
  .apartment-price {
    align-self: flex-end;
  }
}

// 底部间距
.bottom-spacer {
  height: 80px;
}

// 操作栏
.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(15px);
  padding: 20px;
  border-top: 1px solid rgba(139, 92, 246, 0.1);
  z-index: 1000;
  box-shadow: 0 -4px 20px rgba(102, 126, 234, 0.1);
}

.appointment-btn {
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(39, 121, 65, 0.3);
  
  .btn-icon {
    margin-right: 8px;
  }
}

// 响应式调整
@media (max-width: 375px) {
  .facility-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .basic-info-grid {
    grid-template-columns: 1fr;
  }
}

// 轮播图指示器样式
:deep(.van-swipe__indicator) {
  background-color: rgba(255, 255, 255, 0.5);
  
  &.van-swipe__indicator--active {
    background-color: #8b5cf6;
  }
}
</style>
